import request from "@/utils/axios"
const api_name = '/users'


//请求拦截器 在请求头中加token
request.interceptors.request.use(
    config => { // 将获取到的token设置给header中的token
        console.log("1Token为:" + localStorage.getItem('token'));

        const whiteList = ['https://restapi.amap.com/v3/config/district']
        console.log(config.url);
        if (whiteList.includes(config.url)) {
            console.log(config.url);
            // 如果在白名单中，则不添加token
            return config;
        }
        else{
            if (localStorage.getItem('token')) {
                console.log("2Token为:" + localStorage.getItem('token'));
                config.headers.token = localStorage.getItem('token');
                console.log("请求头为:" + config.headers);
            }
            return config;
        }
        
    },
    error => {
        return Promise.reject(error);
    }
)


export default {
    login(userInfo) {
        return request({
            url: '/login',
            method: 'post',
            data: userInfo
        })
    },
    register(userInfo) {
        return request({
            url: '/register',
            method: 'post',
            data: userInfo
        })
    },
    forget(userInfo) {
        return request({
            url: '/forget',
            method: 'put',
            data: userInfo
        })
    },
    get(userInfo) {
        return request({
            url: api_name,
            method: 'get',
            params: userInfo
        })
    },
    newUser(userInfo) {
        return request({
            url: api_name,
            method: 'post',
            data: userInfo
        })
    },
    changeUser(userInfo) {
        return request({
            url: api_name,
            method: 'put',
            data: userInfo
        })
    },
    deleteUser(id) {
        return request({
            url: api_name + '/' + id,
            method: 'delete'
        })
    },
    getCityList(cityInfo) {
        return request({
            url: 'https://restapi.amap.com/v3/config/district',
            method: 'get',
            params: cityInfo
        })
    }


}